package ikar.nrd.fillingref.generator;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class CoreUtils {

	public static Document getXmlDocument(InputStream inStream) {
		DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
		dFactory.setNamespaceAware(true);
		DocumentBuilder builder;
		try {
			builder = dFactory.newDocumentBuilder();
			Document document = builder.parse(inStream);
			return document;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String coalesce(String src, String replaceWith) {
		return isEmpty(src) ? replaceWith : src;
	}

	public static boolean isEmpty(String src) {
		return src == null || src.isEmpty();
	}

	public static String getColredValue(String value, String color, Integer size) {
		return "<font color=" + color + " size=" + size + ">" + value + "</font>";
	}
}
